<?php
dump($data);
?>
<!DOCTYPE html>
<html lang="zhcn" style="font-size:50px">
<head>
    <include file="Common/meta" />
    <title>编辑作品 - {$_ENV['site'][0]['title']}</title>
    <include file="Common/link" />
</head>
<body>
<div class="layout-header">
    <div class="container">
        <h1 class="title">编辑作品</h1>
        <a class="header-back" href="/user/personCenter"><i class="icon icon-left"></i></a>
    </div>
</div>
<div class="layout-body">
    <div class="container">
        <section class="work-edit">

            <h1 class="work-header">名称</h1>
            <input class="work-name" type="text" value="{$data['production']['product_name']}">

            <div class="work-type" id="work-type">
                <span class="work-header">类型</span>
                <input value="附件" type="button" class="work-type-btn <if condition="$product['product_type'] eq 1"> active</if>">
                <input value="外部链接" type="button" class="work-type-btn <if condition="$product['product_type'] eq 0"> active</if>" >
            </div>

            <div class="work-choose">
                <input type="file" value="选择附件" id="work-file">
                <span class="work-limit">附件仅支持 jpg,png,pdf,ppt,pptx,doc,docx</span>
            </div>
            <button class="btn btn-primary work-submit" id="work-submit">确定</button>
        </section>
    </div>
</div>
</body>
<include file="Common/script" />
<script>
    var page = {
        product_type:{$data['production']['product_type']},
        product_id:{$data['production']['product_id']};
    }
    // 切换作品类型
    $('#work-type .work-type-btn').on('click', function () {
        if ($(this).val() !== page.product_type) { // 附件
            $('.work-type-btn').removeClass('active');
            $(this).addClass('active');

        }
    })
    // 编辑作品保存
    $('#work-submit').on('click', function (e) {
        e.preventDefault();

        var type = $('#work-dialog-type').val();
        var name = $('#work-dialog-name').val();
        if (!name) {
            eui.msg('请输入作品名称');
            return;
        }

        var formData = new FormData();

        if (type == 0 || type == 1) { // 附件

            var files = $('#work-file')[0].files;
            if (isAdd && (!files || !files.length)) {
                eui.msg('请上传附件');
                return;
            }

            var fileName = $('#work-dialog-file').val();
            var dot = fileName.lastIndexOf('.');
            var type = fileName.toLowerCase().substring(dot + 1);
            if ('jpg|png|pdf|ppt|pptx|doc|docx|pptx'.indexOf(type) == -1) {
                eui.msg('附件格式不正确');
                return;
            }

            if (files[0].size > 2 * 1024 * 1024) {
                eui.msg('附件不能大于2M');
                return;
            }

            formData.append('file', files[0]);

        } else {
            var link = $('#work-dialog-link').val();
            if (!link) {
                eui.msg('请输入外部链接');
                return;
            }

            function isUrl(url){
                var exp = '^https?://([0-9a-zA-Z-]+\.)+[a-zA-Z]{2,8}'
                        + '(/([0-9a-zA-Z-_\.]+)*?)*' // 文件
                        + '[\\d\\D]*'
                        //+ '(#[0-9a-zA-Z-_]??)?' // hash
                        + '$';
                var reg = new RegExp(exp);
                return reg.test(url);
            }

            if (!isUrl(link)) {
                eui.msg('链接格式不正确');
                return;
            }

            formData.append('file', link);
        }

        formData.append('product_name', name);

        var url;
        if (isAdd) {
            url = '/stdproduct/add';
        } else {
            url = '/stdproduct/update';
            formData.append('product_id', $operateItem.data('id'));
        }

        $.ajax({
            url: url,
            data: formData,
            type: 'POST',
            dataType: 'json',
            processData: false,
            contentType: false,
            success: function (obj) {
                if (obj.code === 0) {
                    eui.msg('保存成功');
                    setTimeout(function () {
                        location.reload(true);
                    }, 800);
                } else {
                    eui.msg('修改失败');
                }

            },
            error: function () {
                eui.alert('系统出错');
            }
        })
    });
</script>
</html>